package camid.services.nodemanager.observers;

import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import camid.distribution.events.TimerEvent;
import camid.services.nodemanager.NodeManager;
import camid.util.monitoring.SigarLoader;

public class SystemUsageObserver extends AbstractNodeObserver {

	
	
	
	private Sigar sigar;

	public SystemUsageObserver() 
	{
		init();
	}
	
	
	@Override
	public void update(NodeManager nm, Object event) 
	{
		if( event instanceof TimerEvent )
		{
			init();
			try {
				Double idle = sigar.getCpuPerc().getIdle();
				Double memory = sigar.getMem().getUsedPercent();
				nm.setCpuUsage(1.0 - idle);
				nm.setMemoryUsage(memory);
			} catch (SigarException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void init() 
	{
		if(sigar == null)
		{
			this.sigar = SigarLoader.getInstance().getSigar();
		}
		
	}

	public static void main(String[] args) throws SigarException {
		
		Sigar sigar = SigarLoader.getInstance().getSigar();
		System.out.println(sigar.getCpuPerc().getIdle());
		
	}
	
}
